package com.td.erhome.ui.sellhouse;

import android.app.Dialog;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.td.erhome.R;
import com.td.erhome.adapter.WritrBookAdapter;
import com.td.erhome.base.BaseActivity;
import com.td.erhome.models.BaseBean;
import com.td.erhome.models.FloorBean;
import com.td.erhome.models.HouseIdBean;
import com.td.erhome.models.KeyStribgBean;
import com.td.erhome.models.PayMethod;
import com.td.erhome.net.Api;
import com.td.erhome.net.BaseSubscriber;
import com.td.erhome.utils.ConstUtils;
import com.td.erhome.utils.DialogUtils;
import com.td.erhome.utils.JsonParser;
import com.td.erhome.utils.PayUtils;
import com.td.erhome.utils.RxBus;
import com.td.erhome.utils.T;
import com.td.erhome.utils.TextUtil;
import com.td.erhome.utils.TypeUtils;
import com.td.erhome.utils.UserManager;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;
import me.iwf.photopicker.PhotoPicker;
import me.iwf.photopicker.PhotoPreview;
import me.shaohui.advancedluban.Luban;
import me.shaohui.advancedluban.OnMultiCompressListener;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import static com.td.erhome.R.id.progressBar1;


public class SecondActivity extends BaseActivity implements WritrBookAdapter.OnRecyclerViewItemClickListener {


    @Bind(R.id.tv_lc)
    TextView mTvLc;
    @Bind(R.id.lin_lc)
    LinearLayout mLinLc;
    @Bind(R.id.tv_zx)
    TextView mTvZx;
    @Bind(R.id.tv_wy)
    TextView mTvWy;
    @Bind(R.id.tv_dk)
    TextView mTvDk;
    @Bind(R.id.tv_cx)
    TextView mTvCx;
    @Bind(R.id.lin_zx)
    LinearLayout mLinZx;
    @Bind(R.id.lin_cx)
    LinearLayout mLinCx;
    @Bind(R.id.lin_dk)
    LinearLayout mLinDk;
    @Bind(R.id.lin_wy)
    LinearLayout mLinWy;
    @Bind(R.id.lin_js)
    RelativeLayout mLinJs;
    @Bind(R.id.lin_pt)
    LinearLayout mLinPt;
    @Bind(R.id.lin_time)
    LinearLayout mLinTime;
    @Bind(R.id.recyclview)
    RecyclerView mRecycview;
    @Bind(R.id.tv_pt)
    TextView mTvPt;
    List<String> data = new ArrayList<>();
    @Bind(R.id.et_building_num)
    EditText mEtBuildingNum;
    @Bind(R.id.et_building_unit)
    EditText mEtBuildingUnit;
    @Bind(R.id.et_house_num)
    EditText mEtHouseNum;
    @Bind(R.id.lin_zl)
    LinearLayout mLinZl;
    @Bind(R.id.tv_time)
    TextView mTvTime;
    @Bind(R.id.tv_pic_num)
    TextView mTvPicNum;
    @Bind(R.id.img_xyb)
    ImageView mTdXyb;
    @Bind(R.id.td_back)
    ImageView mTdBack;
    @Bind(R.id.img_is_accept_loan)
    ImageView mImgIsAcceptLoan;
    @Bind(R.id.tv_mark)
    TextView mTvMark;
    @Bind(R.id.tv_jd)
    TextView mTvJd;
    @Bind(progressBar1)
    ProgressBar mProgressBar;
    private WritrBookAdapter mAdapter;
    private ArrayList<String> paths = new ArrayList<>();
    private Subscription mSubscribe;
    private String mHouse_id;
    private Subscription mSubscribe1;
    private String mPointLc="--";
    private String mHotelLc="--";
    private DialogUtils mDialogUtils;
    private Subscription mSubscribe2;
    private String mSplitStr="--";
    List<File> fileList = new ArrayList<>();
    List<RequestBody> requestBodies = new ArrayList<>();
    private FloorBean mFloorBean;
    private int  per = 0; //完善度

    private Subscription mSubscribe3;
    private Subscription mSubscribe4;
    private String is_accept_loan = "2";

    private List<File> mFileList = new ArrayList<>();
    private Dialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_second;
    }
    @Override
    protected void initView() {
        mHouse_id = getIntent().getStringExtra("house_id");
        mDialogUtils = new DialogUtils();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 3);
        mRecycview.setLayoutManager(gridLayoutManager);
        mAdapter = new WritrBookAdapter(this,paths);
        mRecycview.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(this);
        setFooterView(mRecycview);


        mSubscribe1 = RxBus.getInstance().toObserverable(FloorBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<FloorBean>() {
                    @Override
                    public void call(FloorBean floorBean) {
                        mFloorBean = floorBean;
                        mTvLc.setText("第"+floorBean.pointLc+"层--总共"+floorBean.pointLc+"层");
                        mPointLc = floorBean.pointLc;
                        mHotelLc = floorBean.hotelLc;
                    }
                });

        mSubscribe2 = RxBus.getInstance().toObserverable(KeyStribgBean.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<KeyStribgBean>() {
                    @Override
                    public void call(KeyStribgBean keyBean) {
                        String[] split = keyBean.key.split(" ");
                        //配套设施
                        mSplitStr = JsonParser.serializeToJson(split);

                    }
                });

        mSubscribe3 = RxBus.getInstance().toObserverable(PayMethod.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<PayMethod>() {
                    @Override
                    public void call(PayMethod payMethod) {
                        sellHousePublish(payMethod.pay_type);
                    }
                });

        mSubscribe4 = RxBus.getInstance().toObserverable(Integer.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Integer>() {
                    @Override
                    public void call(Integer i) {
                        if (i== ConstUtils.OUT){
                            finish();
                        }}
                });

        mEtBuildingNum.addTextChangedListener(new AddWatcher());
        mEtBuildingUnit.addTextChangedListener(new AddWatcher());
        mEtHouseNum.addTextChangedListener(new AddWatcher());
        mTvLc.addTextChangedListener(new AddWatcher());
        mTvZx.addTextChangedListener(new AddWatcher());
        mTvCx.addTextChangedListener(new AddWatcher());
        mTvDk.addTextChangedListener(new AddWatcher());
        mTvWy.addTextChangedListener(new AddWatcher());
        mTvPt.addTextChangedListener(new AddWatcher());
        mTvTime.addTextChangedListener(new AddWatcher());
        mTvMark.addTextChangedListener(new AddWatcher());

    }


    @Override
    public void onItemClick(int point) {
        mAdapter.mPaths.remove(point);
        mFileList.remove(point);
        mAdapter.notifyDataSetChanged();

        paths.remove(point);

        mTvPicNum.setText("已选择"+mAdapter.mPaths.size()+"张");

        upDataercent();

    }

    private class AddWatcher implements TextWatcher {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            upDataercent();

        }
    }

    private void upDataercent() {
        per=40;
        if (!TextUtil.isEditTextEmpty(mEtBuildingNum)){
            per+=5;
        }
        if (!TextUtil.isEditTextEmpty(mEtBuildingUnit)){
            per+=5;
        }
        if (!TextUtil.isEditTextEmpty(mEtHouseNum)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvLc)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvZx)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvCx)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvDk)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvWy)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvPt)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvTime)){
            per+=5;
        }
        if (!TextUtil.isTextViewEmpty(mTvMark)){
            per+=5;
        }
        if (mAdapter.mPaths.size()!=0){
            per+=5;
        }
        mProgressBar.setProgress(per);
        mTvJd.setText("完善度"+per+"%");
    }


    private void setFooterView(RecyclerView view) {
        View header = LayoutInflater.from(this).inflate(R.layout.write_book_header, view, false);
        ImageView mImgAdd = (ImageView) header.findViewById(R.id.img_add);
        mImgAdd.setOnClickListener(new FootClickListener());
        mAdapter.setFooterView(header);
    }

    @Override
    protected void releaseResource() {
        if (!mSubscribe1.isUnsubscribed()){
            mSubscribe1.unsubscribe();
        }if (!mSubscribe2.isUnsubscribed()){
            mSubscribe2.unsubscribe();
        }if (!mSubscribe3.isUnsubscribed()){
            mSubscribe3.unsubscribe();
        }if (!mSubscribe4.isUnsubscribed()){
            mSubscribe4.unsubscribe();
        }
        mDialogUtils = null;
    }

    @OnClick({R.id.lin_lc, R.id.lin_zx, R.id.lin_cx, R.id.lin_dk, R.id.lin_wy,
            R.id.lin_pt, R.id.lin_time,R.id.img_xyb,R.id.td_back,R.id.tv_mark,R.id.lin_js})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.lin_lc: //楼层
                mDialogUtils.createlcBottom(this, mTvLc,mFloorBean);
                break;
            case R.id.lin_zx: //装修情况
                data.clear();
                data.add("毛坯房");
                data.add("简装");
                data.add("中装");
                data.add("精装");
                data.add("豪装");
                mDialogUtils.createBottom(this, data, mTvZx);
                break;
            case R.id.lin_cx: //房屋朝向
                data.clear();
                data.add("南");
                data.add("东");
                data.add("西");
                data.add("北");
                data.add("东南");
                data.add("西南");
                data.add("东北");
                data.add("西北");
                data.add("南北通透");
                data.add("东西通透");
                mDialogUtils.createBottom(this, data, mTvCx);
                break;
            case R.id.lin_dk: //是否有贷款
                data.clear();
                data.add("是");
                data.add("否");
                mDialogUtils.createBottom(this, data, mTvDk);
                break;
            case R.id.lin_wy: //是否唯一住房
                data.clear();
                data.add("是");
                data.add("否");
                mDialogUtils.createBottom(this, data, mTvWy);
                break;
            case R.id.lin_pt:
                mDialogUtils.createCheckBox(this,mTvPt);
                break;
            case R.id.lin_time:
                mDialogUtils.createTime(this,mTvTime);
                break;
            case R.id.img_xyb:
                compressMultiListener();
                break;
            case R.id.td_back:
                finish();
                break;
            case R.id.tv_mark:
                Intent intent = new Intent(this,MarkActivity.class);
                startActivityForResult(intent,201);
                break;
            case R.id.lin_js:
                if (is_accept_loan.equals("2")){
                    mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_selected);
                    is_accept_loan="1";
                }else {
                    mImgIsAcceptLoan.setImageResource(R.mipmap.sellers_issue_icon_default);
                    is_accept_loan="2";
                }
                break;
        }
    }

   /* @Override
    public void onBackPressed() {
        if (paths.size() == 0) {
            finish();
        } else if (mAdapter.isShow) {
            mAdapter.isShow = false;
            mAdapter.notifyDataSetChanged();
        } else {
            paths.clear();
            mAdapter.mPaths.clear();
            mAdapter.notifyDataSetChanged();
        }
    }
*/



    //进入选择图片
    private class FootClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            mAdapter.isShow = false;
            mAdapter.notifyDataSetChanged();

            PhotoPicker.builder()
                    .setSelected(paths)
                    .setPhotoCount(6)
                    .start(SecondActivity.this);
    }
        }

    /**
     * 图片选择回调
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK &&
                (requestCode == PhotoPicker.REQUEST_CODE || requestCode == PhotoPreview.REQUEST_CODE)) {
            if (data != null) {
                mFileList.clear();
                mAdapter.mPaths.clear();
                paths = data.getStringArrayListExtra(PhotoPicker.KEY_SELECTED_PHOTOS);
                int length = paths.size();
                mTvPicNum.setText("已选择"+length+"张");
                if (length == 0) {
                    upDataercent();
                    return;
                } else {
                    mAdapter.addAllData(paths);
                    upDataercent();
                    for (String str : paths) {
                        mFileList.add(new File(str));
                    }
                }
            }
        }
        if (requestCode==201&&resultCode == RESULT_OK){
            mTvMark.setText(data.getStringExtra("mark"));
        }
    }

    private void compressMultiListener() {
        if (mFileList.size()!=6) {
           T.showLong(this,"请上传6张图片");
            return;
        }
        Luban.compress(this, mFileList)
                .putGear(Luban.FIRST_GEAR)
                .launch(new OnMultiCompressListener() {
                    @Override
                    public void onStart() {
                        if (mDialog ==null){
                            mDialog = new DialogUtils().loading(SecondActivity.this);
                        }
                        mDialog.show();

                    }

                    @Override
                    public void onSuccess(List<File> fileList) {
                        int size = fileList.size();
                        while (size-- > 0) {
                            RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), fileList.get(size));
                            requestBodies.add(fileBody);
                        }
                        upPicture();

                    }

                    @Override
                    public void onError(Throwable e) {
                        e.printStackTrace();
                    }
                });
    }



    /** 图片上传
     */
    private void upPicture() {
        postData(mFileList,requestBodies);
    }

    private void postData(List<File> fileList, List<RequestBody> requestBodies) {
        MultipartBody boy = null;
        if (fileList.size() == 0){
            boy = new MultipartBody.Builder()
                    .setType(MultipartBody.FORM)
                    .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                    .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                    .addFormDataPart("house_id", mHouse_id)
                    .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                    .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                    .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                    .addFormDataPart("house_floor", mPointLc) //楼层
                    .addFormDataPart("total_floor", mHotelLc) //总楼层
                    .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                    .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                    .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                    .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                    .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                    .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                    .addFormDataPart("facilities", mSplitStr) //配套设施
                    .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                    .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                    .build();
        }
   else if (fileList.size() == 1) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                 .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .build();
    } else if (fileList.size() == 2) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼好
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .build();
    } else if (fileList.size() == 3) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .build();
    } else if (fileList.size() == 4) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .build();
    } else if (fileList.size() == 5) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                .build();
    } else if (fileList.size() == 6) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                .build();
    } else if (fileList.size() == 7) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                .build();
    } else if (fileList.size() == 8) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                .addFormDataPart("house_id", mHouse_id)
                .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                .addFormDataPart("house_floor", mPointLc) //楼层
                .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan) //总楼层
                .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                .addFormDataPart("facilities", mSplitStr) //配套设施
                .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                .addFormDataPart("house_img[]", fileList.get(7).getName(), requestBodies.get(7))
                .build();
    } else if (fileList.size() == 9) {
        boy = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("user_id", UserManager.getIns().getUser().uid+"")
                    .addFormDataPart("token", UserManager.getIns().getUser().access_token)
                    .addFormDataPart("house_id", mHouse_id)
                    .addFormDataPart("building_num", mEtBuildingNum.getText().toString()) //楼层
                    .addFormDataPart("building_unit", mEtBuildingUnit.getText().toString()) //单元
                    .addFormDataPart("house_num", mEtHouseNum.getText().toString()) //门牌号
                    .addFormDataPart("house_floor", mPointLc) //楼层
                    .addFormDataPart("total_floor", mHotelLc) //总楼层
                .addFormDataPart("is_accept_loan", is_accept_loan)
                    .addFormDataPart("decCondition", TypeUtils.getFloorInt(mTvZx.getText().toString())) //装修情况
                    .addFormDataPart("orientations", TypeUtils.getorientationInt(mTvCx.getText().toString())) //房屋朝向
                    .addFormDataPart("is_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否有贷款
                    .addFormDataPart("is_unique", TypeUtils.getBooleInt(mTvWy.getText().toString())) //是否唯一住房
                    .addFormDataPart("is_accept_loan", TypeUtils.getBooleInt(mTvDk.getText().toString())) //是否接受卖家贷款
                    .addFormDataPart("facilities", mSplitStr) //配套设施
                    .addFormDataPart("buy_time", mTvTime.getText().toString()) //购买时间
                .addFormDataPart("house_owner_remark",mTvMark.getText().toString())
                .addFormDataPart("house_img[]", fileList.get(0).getName(), requestBodies.get(0))
                .addFormDataPart("house_img[]", fileList.get(1).getName(), requestBodies.get(1))
                .addFormDataPart("house_img[]", fileList.get(2).getName(), requestBodies.get(2))
                .addFormDataPart("house_img[]", fileList.get(3).getName(), requestBodies.get(3))
                .addFormDataPart("house_img[]", fileList.get(4).getName(), requestBodies.get(4))
                .addFormDataPart("house_img[]", fileList.get(5).getName(), requestBodies.get(5))
                .addFormDataPart("house_img[]", fileList.get(6).getName(), requestBodies.get(6))
                .addFormDataPart("house_img[]", fileList.get(7).getName(), requestBodies.get(7))
                .addFormDataPart("house_img[]", fileList.get(8).getName(), requestBodies.get(8))
                .build();
    }

        Api.getRetrofit().addSellHouseStep2(boy)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseBean<HouseIdBean>>(SecondActivity.this){
                    @Override
                    public void onStart() {
                    }

                    @Override
                    public void onCompleted() {
                        if (mDialog!=null && mDialog.isShowing()){
                            mDialog.dismiss();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (mDialog!=null && mDialog.isShowing()){
                            mDialog.dismiss();
                        }
                        T.showLong(SecondActivity.this,e.getMessage());
                    }
                    @Override
                    public void onNext(BaseBean<HouseIdBean> houseIdBean) {
                        super.onNext(houseIdBean);
                        if (mDialog!=null && mDialog.isShowing()){
                            mDialog.dismiss();
                        }
                        mDialogUtils.initPopGetInfo(SecondActivity.this,2);
                    }
                });
    }

    //获取预支付信息
    public void sellHousePublish(String pay_type){
        PayUtils payUtils = new PayUtils(this);
        HashMap<String, Object> params = new HashMap<>();
        params.put("house_id", mHouse_id);
        params.put("pay_type",pay_type);
        params.put("user_id", UserManager.getIns().getUser().uid);
        payUtils.getPrePay(params);

        UserManager.getIns().saveUserPer(per);

    }

}
